
package com.njkwt.business.system.service;

import com.njkwt.business.system.domain.Syslog;
import com.njkwt.business.system.domain.SyslogExample;
import com.njkwt.business.system.dto.SyslogDto;
import com.njkwt.business.system.dto.PageDto;
import com.njkwt.business.system.mapper.SyslogMapper;
import com.njkwt.util.CopyUtil;
import com.njkwt.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Date;

/**
* @ClassName  SyslogService
* @Description
* @Author ydj (171799383@qq.com)
* @Date 2020-8-18 14:42:15
* @Version V1.0
**/

@Service
public class SyslogService {

    @Resource
    private SyslogMapper syslogMapper;

    /**
     * 列表查询
     */
    public void list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        SyslogExample syslogExample = new SyslogExample();
        List<Syslog> syslogList = syslogMapper.selectByExample(syslogExample);
        PageInfo<Syslog> pageInfo = new PageInfo<>(syslogList);
        pageDto.setTotal(pageInfo.getTotal());
        List<SyslogDto> syslogDtoList = CopyUtil.copyList(syslogList, SyslogDto.class);
        pageDto.setList(syslogDtoList);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    public void save(SyslogDto syslogDto) {
        Syslog syslog = CopyUtil.copy(syslogDto, Syslog.class);
        if (StringUtils.isEmpty(syslogDto.getId())) {
            this.insert(syslog);
        } else {
            this.update(syslog);
        }
    }

    /**
     * 新增
     */
    private void insert(Syslog syslog) {
        Date now = new Date();
        syslog.setId(UuidUtil.getShortUuid());
        syslogMapper.insert(syslog);
    }

    /**
     * 更新
     */
    private void update(Syslog syslog) {
        syslogMapper.updateByPrimaryKey(syslog);
    }

    /**
     * 删除
     */
    public void delete(String id) {
        syslogMapper.deleteByPrimaryKey(id);
    }
}
